﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

using EHTWebsite.Code;

/// <summary>
/// Author toanvt
/// CreatedDate 08/12/2011
/// </summary>
public partial class Administrator_AdminVideoIU : System.Web.UI.Page
{
    public Int32 videoID = -1;
    private String message = "";

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            if (Request.QueryString["Message"] != null)
            {
                try
                {
                    message = Request.QueryString["Message"].ToString();
                }
                catch (Exception)
                {
                    message = "";
                }
            }

            //hiển thị Message nếu có giá trị Message gửi sang từ màn hình nguồn
            this.lbMessage.Text = message;
            this.lbMessage.CssClass = "BlueCorlor";

            try
            {
                videoID = Int32.Parse(Request.QueryString["VideoID"].ToString());
            }
            catch
            {
                videoID = -1;
            }

            if (videoID == -1)
            {
                this.lbAdminVideoIU.Text = "Thêm mới video";
                ViewState["action"] = HangSo.ACTION_ADD;
            }
            else
            {
                this.lbAdminVideoIU.Text = "Sửa video";
                Video theVideo = VideoProvider.GetVideoByVideoID(videoID);

                if (theVideo != null)
                {
                    this.txtMoTaOther.Text = theVideo.MoTaOther;
                    this.txtMoTaVN.Text = theVideo.MoTaVN;
                    this.txtVideoUrl.Text = theVideo.VideoUrl;
                    this.txtNumberOfOrder.Text = theVideo.NumberOfOrder.ToString();
                    this.ckPublish.Checked = theVideo.Publish;
                    ViewState["VideoID"] = theVideo.VideoID;
                    ViewState["action"] = HangSo.ACTION_UPDATE;
                }
            }
        }
    }

    protected void btnSave_Click(object sender, EventArgs e)
    {
        this.lbMessage.Text = "";
        this.lbMessage.CssClass = "";

        Page.Validate();

        if (Page.IsValid)
        {
            Int32 videoIDOut = -1;
            Video theVideo = new Video();
            theVideo.MoTaOther = this.txtMoTaOther.Text;
            theVideo.MoTaVN = this.txtMoTaVN.Text;
            theVideo.VideoUrl = this.txtVideoUrl.Text.Trim();
            theVideo.Publish = this.ckPublish.Checked;
      
            try
            {
                theVideo.NumberOfOrder = Int32.Parse(this.txtNumberOfOrder.Text.Trim());
            }
            catch (Exception ex)
            {
                theVideo.NumberOfOrder = 1;
                this.txtNumberOfOrder.Text = "1";
            }

            String action = ViewState["action"].ToString();

            if (action == HangSo.ACTION_ADD)
            {
                theVideo.VideoID = -1;
                videoIDOut = VideoProvider.IUVideos(theVideo, true);
            }
            else
            {
                theVideo.VideoID = Int32.Parse(ViewState["VideoID"].ToString());
                videoIDOut = VideoProvider.IUVideos(theVideo, false);
            }

            Response.Redirect("AdminVideos.aspx");
        }
    }

    protected void btnBack_Click(object sender, EventArgs e)
    {
        Response.Redirect("AdminVideos.aspx");
    }

    protected void btnApply_Click(object sender, EventArgs e)
    {
        this.lbMessage.Text = "";
        this.lbMessage.CssClass = "";

        Page.Validate();

        if (Page.IsValid)
        {
            Int32 videoIDOut = -1;
            Video theVideo = new Video();
            theVideo.MoTaOther = this.txtMoTaOther.Text;
            theVideo.MoTaVN = this.txtMoTaVN.Text;
            theVideo.VideoUrl = this.txtVideoUrl.Text.Trim();
            theVideo.Publish = this.ckPublish.Checked;

            try
            {
                theVideo.NumberOfOrder = Int32.Parse(this.txtNumberOfOrder.Text.Trim());
            }
            catch (Exception ex)
            {
                theVideo.NumberOfOrder = 1;
                this.txtNumberOfOrder.Text = "1";
            }

            String action = ViewState["action"].ToString();

            if (action == HangSo.ACTION_ADD)
            {
                theVideo.VideoID = -1;
                videoIDOut = VideoProvider.IUVideos(theVideo, true);
            }
            else
            {
                theVideo.VideoID = Int32.Parse(ViewState["VideoID"].ToString());
                videoIDOut = VideoProvider.IUVideos(theVideo, false);
            }

            Response.Redirect("AdminVideoIU.aspx?VideoID=" + videoIDOut.ToString() + "&Message=Cập nhật thành công");
        }
    }
}
